#include <stdio.h>
#include <stdlib.h>
// #include "../inc/DataBean.h"
// #include "../inc/NativeLibrary.h"
#include "Register.h"
#include "DataBean.h"
#include "com_wish_jni_NativeLibrary.h"
#include <jni.h>
#include "protocalParsing.h"
// extern   unsigned char  wishProtocalPasing( jni_data_bean data_bean,unsigned char *inputDataArray,int inputDataLen);
extern struct jni_data_bean_t wishProtocalPasing(char inputDataArray[], int inputDataLen);
jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
  JNIEnv *env = NULL;
  jint result = JNI_ERR;
  if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_4) != JNI_OK)
  {
    return result;
  }
  printf("enter JNI_OnLoad\n");
  register_classes(env); // 注册所有的类
  return JNI_VERSION_1_6;
}

JNIEXPORT jobject JNICALL
Java_com_wish_jni_NativeLibrary_getDataFromNative(JNIEnv *env, jclass type)
{
  jni_data_bean data_bean;
  jobject obj = data_bean_c_to_java(env, &data_bean);
  return obj;
}

JNIEXPORT void JNICALL Java_com_wish_jni_NativeLibrary_test(JNIEnv *env, jclass cl)
{
  printf("this is Java_NativeLibrary_test\n");
}

JNIEXPORT jobject JNICALL Java_com_wish_jni_NativeLibrary_protocalParsing(JNIEnv *env, jclass cl, jbyteArray jInputArrData)
{

  jbyte *array = (*env)->GetByteArrayElements(env, jInputArrData, NULL);

  int sum = 0;
  int len = (*env)->GetArrayLength(env, jInputArrData);
  printf(" len  %d ", len);
  unsigned char *cArray = (unsigned char *)malloc(len);
  // printf("recv arr=");
  for (int i = 0; i < len; i++)
  {
    cArray[i] = array[i];
    // printf(" i= %d ", cArray[i]);
  }
  printf("\n");

  jni_data_bean data_bean = wishProtocalPasing(cArray, len);
  printf(" data_bean.version =%x ", data_bean.version);

  (*env)->ReleaseByteArrayElements(env, jInputArrData, array, 0);
  free(cArray);
  jobject obj = data_bean_c_to_java(env, &data_bean);
  return obj;
}

JNIEXPORT jboolean JNICALL Java_com_wish_jni_NativeLibrary_formProtocalPk(JNIEnv *env, jclass cl, jobject jDataBeanIn)
{
  jni_data_bean cData_bean;
  data_bean_java_to_c(env, jDataBeanIn, &cData_bean);

  printf(" cData_bean.version= %x ", cData_bean.version);

  COM_COM_TYPE *pCom;
  UNION_4_BYTE tmp4;
  UNION_2_BYTE tmp2;

  pCom = (COM_COM_TYPE *)&cData_bean.formPkArray[0];
  pCom->head.startFlag = mqCmd_startFlag;

  tmp2.word = cData_bean.pkId;
  pCom->head.pkId[0] = tmp2.byte[0];
  pCom->head.pkId[1] = tmp2.byte[1];
  pCom->head.packetCmd = cData_bean.pkCmd;
  pCom->head.packetAnswer = cData_bean.pkAck;
  int idx = HEAD_LEN;
  if (pCom->head.packetCmd == mqCmd_getSeriesData)
  {
    for (int i = 0; i < cData_bean.paraCnt; i++)
    {
      tmp2.word = cData_bean.oid[i];
      pCom->buffer[idx++] = tmp2.byte[0];
      pCom->buffer[idx++] = tmp2.byte[1];
    }
  }
  if (pCom->head.packetCmd == mqCmd_diffDataReport)
  {
    for (int i = 0; i < 3; i++)
    {
      tmp4.lword = cData_bean.val[i];
      pCom->buffer[idx++] = tmp4.byte[0];
      pCom->buffer[idx++] = tmp4.byte[1];
    }
  }
  else
  {
    for (int i = 0; i < cData_bean.paraCnt; i++)
    {
      tmp2.word = cData_bean.ol[i] ;
      pCom->buffer[idx++] = tmp2.byte[0];
      pCom->buffer[idx++] = tmp2.byte[1];
      tmp2.word = cData_bean.oid[i];
      pCom->buffer[idx++] = tmp2.byte[0];
      pCom->buffer[idx++] = tmp2.byte[1];
      tmp4.lword = cData_bean.val[i];
      if (cData_bean.ol[i] >= 5)
        pCom->buffer[idx++] = tmp4.byte[0];
      if (cData_bean.ol[i] >= 6)
        pCom->buffer[idx++] = tmp4.byte[1];
      if (cData_bean.ol[i] >= 7)
        pCom->buffer[idx++] = tmp4.byte[2];
      if (cData_bean.ol[i] >= 8)
        pCom->buffer[idx++] = tmp4.byte[3];
    }
  }

  tmp4.lword = idx - 1;
  pCom->head.pkLen[0] = tmp4.byte[0];
  pCom->head.pkLen[1] = tmp4.byte[1];
  pCom->head.pkLen[2] = tmp4.byte[2];
  pCom->head.pkLen[3] = tmp4.byte[3];

  uint8 calSum = 0;
  for (int i = 0; i < idx; i++)
  {
    calSum += (uint8)cData_bean.formPkArray[i + 1];
  }
  cData_bean.formPkArray[idx++] = calSum;
  cData_bean.formPkArray[idx++] = mqCmd_endFlag;
  cData_bean.pkLen = idx;

  updateFormPk_c_to_java(env, jDataBeanIn, &cData_bean);

  return 1;
}